// pages/agreementCustomerEdit/agreementCustomerEdit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bistype: "", 
    companyname: "",
    address: '',
    legalname: '',
    legalmobile: '',
    legalcardno: '',
    postername: '',
    postermobile: '',
    postercompany: '',
    bisendtime: '',
    bisstarttime: '',
    isDisabled: true,
    customer_id: '',
    show: false,
    date: '',
    pickerConfig: { // 时间筛选
      endDate: true,
      dateLimit: false,
      initStartTime: "2019-01-01",
      initEndTime: "2019-12-01",
      limitStartTime: "2000-01-01",
      limitEndTime: "2100-12-31"
    },
    isEdit: false
  },
  onDisplay(){
    this.setData({
      show: true
    })
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  changeName(e){
    let {
      value
    } = e.detail, {
      filter
    } = e.currentTarget.dataset;
    let params = {};
    params[filter] = value;
    this.setData(params)
  },
  formatDate(date) {
    date = [new Date(date)];
    return `${date[0].getFullYear()}-${date[0].getMonth() + 1}-${date[0].getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      bisstarttime: this.formatDate(event.detail.startTime),
      bisendtime: this.formatDate(event.detail.endTime),
    });
  },
  editCustomer(){
    let ajaxPost = wx.$api.createPorotocolMember,
    params = {
      bistype: this.data.bistype, 
      companyname: this.data.companyname,
      address: this.data.address,
      legalname: this.data.legalname,
      legalmobile: this.data.legalmobile,
      legalcardno: this.data.legalcardno,
      postername: this.data.postername,
      postermobile: this.data.postermobile,
      postercompany: wx.getStorageSync('company_id'),
      bisendtime: new Date(this.data.bisendtime + ' 23:59:59').getTime()/1000,
      bisstarttime: new Date(this.data.bisstarttime + ' 00:00:00').getTime()/1000,
    }
    if(this.data.isEdit){
      ajaxPost = wx.$api.updatePorotocolMember;
      params['id'] = this.data.id
    }
    ajaxPost(params).then(res=>{
      if(res.data.code == 200){
        wx.navigateBack()
      }else{
        wx.$Toast(res.data.msg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.info != 'undefined'){
      let info = JSON.parse(options.info);
      this.setData({
        bistype: info.bistype, 
        companyname: info.companyname,
        address: info.address,
        legalname: info.legalname,
        legalmobile: info.legalmobile,
        legalcardno: info.legalcardno,
        postername: info.postername,
        postermobile: info.postermobile,
        bisendtime: this.formatDate(info.bisendtime*1000),
        bisstarttime: this.formatDate(info.bisstarttime*1000),
        id: info.id,
        isEdit: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})